package com.example.tallybook;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.tallybook.databinding.ActivityMainBinding;

import org.w3c.dom.Text;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private List<Bean> list;
private SQLiteDatabase sqLiteDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setTitle("记账本");

        //实例化数组
        list = new ArrayList<>();
        //打开数据库
        sqLiteDatabase = new MySqlHold(this).getWritableDatabase();

       //记账按钮
        binding.jizhang.setOnClickListener(v -> {
            Intent intent = new Intent(this,JiZhangActivity.class);
            startActivity(intent);
        });
        //查看按钮
        binding.gengduo.setOnClickListener(v -> {
            //查询数据库结果
            seek();
            Intent intent = new Intent(this,JiLuActivity.class);
            intent.putExtra("date",(Serializable)list);
            startActivity(intent);
        });

    }
    @SuppressLint("Range")
    private void seek(){
        list.clear();
        Cursor cursor = sqLiteDatabase.query("jilu",null,null,null,null,null,null);
        int i = 0;

        while (cursor.moveToNext()){
            Bean bean = new Bean();
            bean.setId(i++);
            bean.setSpinn(cursor.getString(cursor.getColumnIndex("spinn")));
            bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
            bean.setDidian(cursor.getString(cursor.getColumnIndex("didian")));
            bean.setMoney(cursor.getString(cursor.getColumnIndex("money")));
            bean.setBeizhu(cursor.getString(cursor.getColumnIndex("beizhu")));

            list.add(bean);
        }
    }


}